//var single_name_old;
var mutex_input_dblclick = 0;
//element-global
$(function(){
    show_revise_change = function(that){
        $(that).children(".row-hover").hide();
        $(that).children(".control-sub").show();
        return true;
    };
    /*
    $(".row-hover .controls-text-small").live("dblclick",function(){
        $(this).parent().parent().children(".span1-compact").children().children(".icon-edit").trigger("click");
    });
    */
    //$(".row-hover .icon-edit").live("click",function(){
    //用于edit
    $(".row-hover span>i.icon-edit").live("click",function(){
        //name的修改
        //alert("icon-edit2")
        if(mutex_input_dblclick) return;
        else mutex_input_dblclick=1;
        //改变显示方式
        show_revise_change($(this).parent().parent().parent().parent());
    });
    //用于edit
    $(".row-model span>i.icon-edit").live("click",function(){
        var this_group = $(this).parent().parent().parent().parent().parent().parent();
        var relationType =  $(this_group).children(".accordion-heading").children().children().children().eq(0).children("p").children("strong").text();
        var relationName =  $(this_group).children(".accordion-heading").children().children().children().eq(0).children("p").children("cite").text();
        m_element.relation = {
            type : relationType,
            name : relationName,
            id : $(this_group).children("input[type='hidden']").val(),
            item : $(this_group)
        };
        $('#modal-attribute-revise').modal('show');
    });

    $('#modal-attribute-revise').on('show', function () {
        $("#modal-attribute-revise").children(".modal-body").children(".btn-group").children("button").html(m_element.relation.type);
        var ul = $("#modal-attribute-revise").children(".modal-body").children(".btn-group").children("ul");
        $(ul).children().remove();
        $("#modal-attribute-revise").children(".modal-body").children("input[type=text]").val(m_element.relation.name);
    });
    //element-name
    $(".ifm-element-input-cancel").live("click",function(){
        var single_name_old = $(this).parent().parent().parent().parent().children(".row").children("div").children("p").eq(0).text();
        if(single_name_old.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        $(this).parent().parent().children("input[type='text']").val(single_name_old);

        $(this).parent().parent().parent().hide();
        $(this).parent().parent().parent().parent().children(".row").show();
        mutex_input_dblclick = 0;
    });

    $(".ifm-element-input-name-submit").live("click",function(){
        var value = revise_show_change_submit($(this),null,false);
        //后台部分
        var data = {
            process : 'revise',
            relation : value.type,
            source: {
                id : m_element.id,
                type : m_element.type
            },
            new:value.new,
            old:value.old
        };
        ajax_relationRevise(data);
    });
    //element-description
    $(".ifm-element-input-desc-cancel").live("click",function(){
        var single_name_old = $(this).parent().parent().parent().parent().children(".row").children("div").children("p").eq(0).text();
        if(single_name_old.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        $(this).parent().parent().children("textarea").val(single_name_old);

        $(this).parent().parent().parent().hide();
        $(this).parent().parent().parent().parent().children(".row").show();
        mutex_input_dblclick = 0;
    });

    $(".ifm-element-input-desc-submit").live("click",function(){
        //var value = revise_show_change_submit($(this),null,false);
        //var value = {state:false};
        var value = {};
        value.new = $(this).parent().parent().children("textarea").val();
        value.type = $(this).parent().parent().parent().children("label").text();
        if(value.new.replace( /^[" "|"　"]*/, "") === '') return value;//去除所有均为空的情况
        //else if(checkLegal){
        //    if(checkExist(dir,value.type,value.new))  return;
        //}
        //value.state = true;

        var text = $(this).parent().parent().parent().parent().children(".row").children("div").children("p").eq(0);
        value.old = $(text).text();
        $(text).text(value.new);
        $(this).parent().parent().parent().hide();
        $(this).parent().parent().parent().parent().children(".row").show();
        mutex_input_dblclick = 0;
        return value;
        //后台部分
    });
});

var reviseElem ={};
//submit
$(function(){
    $("#modal-create").on('show', function(){
        $("#modal-create").children(".modal-header").children("h5").text("Create Child of "+m_element.name);
        $("#modal-create").children(".modal-body").find("input[type='text']").val();
    });
    //cdName
    $(".icd-element-input-cd-submit").live("click",function(){
        var value = revise_show_change_submit($(this),icd['cd'],false);
        if(value.state === true){
            //显示部分
            var head= '<i class="icon-chevron-right" title="click to remove"></i><i class="icon-remove-sign-right"></i>';
            //替换显示
            $(".bs-docs-sidenav .active").children("a").html(head+value.new);
            $(".bs-docs-sidenav .active").attr('name',value.new);
            //存储相关
            $.ajax({
                url: '/post/data/icd-element',
                type: 'post',
                data: {
                    statusArray:statusArray,
                    type : 'cd',
                    process : 'revise',
                    data: {
                        old:value.old,
                        sub :icd['cd'][value.old],
                        new:value.new
                    }
                },
                dataType: 'json',
                success: function(doc){
                    icd = doc.icd;
                    getSourceNav();
                    if(doc.count != 1) return alert("Fail! Please refresh the page and try it again");
                }
            });
        }
    });
    //cd _discription
    $(".icd-element-textarea-submit").live("click",function(){
        var newDescription = $(this).parent().parent().children("textarea").val();
        //if(old === '') return;
        if(newDescription.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        $(this).parent().parent().parent().children("p").text(newDescription);

        $(this).parent().parent().hide();
        $(this).parent().parent().parent().children(".icd-element-textarea").show();
        mutex_input_dblclick = 0;
        $.ajax({
            url: '/post/data/icd-element',
            type: 'post',
            data: {
                statusArray:statusArray,
                type : '_description',
                process : 'revise',
                data: {
                    sub:newDescription
                }
            },
            dataType: 'json',
            success: function(doc){
                if(doc.count != 1) return alert("Fail! Please refresh the page and try it again");
                icd = doc.icd;
                getSourceNav();
            }
        });
    });

    $(".icd-element-textarea-cancel").live("click",function(){
        var old = $(this).parent().parent().parent().children("p").text();
        if(old.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        $(this).parent().parent().children("textarea").val(old);

        $(this).parent().parent().hide();
        $(this).parent().parent().parent().children(".icd-element-textarea").show();
        mutex_input_dblclick = 0;
    });
    //class


    $('#modal-alter-content').on('show', function () {
        //验证信息
        $("#modal-alter-content").children(".modal-header").children("h5").text(reviseElem.label+"：From “"+reviseElem.old.value+"” to “"+reviseElem.new.value+"”");
        $("#modal-alter-content").children(".modal-body").find(".selector").find(".body").children().remove();
        $("#modal-alter-content").children(".modal-body").find(".info").find(".body").children().remove();

        idToIcdDir = {};
        generateIcdIndex(reviseElem.sub.icd,reviseElem.old.id,'class');
        selectLoop(reviseElem.sub.icd,reviseElem.sub.ccd,'class',reviseElem.new,$("#modal-alter-content").children(".modal-body").find(".selector").find(".body"));

    });

    $(".icd-element-input-relation-submit").live("click",function(){
        var class1 = $(this).parent().parent().children().eq(0).children("input[type='text']").val();
        var class2 = $(this).parent().parent().children().eq(1).children("input[type='text']").val();

        if(class1.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        if(class2.replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况

        var relationname = class1 +'-'+class2;
        if(checkExist(icd['relation'],relationname)) return alert("Name aleady Exist");

        var text = $(this).parent().parent().parent().children(".row").children(".span4").children("p");
        var oldName=$(text).text();
        $(text).text(relationname);

        $(this).parent().parent().parent().children().show();
        $(this).parent().parent().hide();
        mutex_input_dblclick = 0;

        var head= '<i class="icon-chevron-right" title="click to remove"></i><i class="icon-remove-sign-right"></i>';
        $(".bs-docs-sidenav .active").children("a").html(head+relationname);

        $.ajax({
            url: '/post/data/icd-element',
            type: 'post',
            data: {
                statusArray:statusArray,
                type : 'relation',
                process : 'revise',
                data: {
                    old:oldName,
                    sub :icd['relation'][statusArray['relation']]['_value'][oldName],
                    new:relationname
                }
            },
            dataType: 'json',
            async: true,
            success: function(doc){
                icd = doc.icd;
                getSourceNav();
                if(doc.count != 1) return alert("Fail! Please refresh the page and try it again");
            }
        });
    });

    $(".icd-element-input-relation-cancel").live("click",function(){
        var relationname = $(this).parent().parent().parent().children(".row").children(".span4").children("p").text();
        relationname = relationname.split("-");
        if(relationname[0].replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况
        if(relationname[1].replace( /^[" "|"　"]*/, "") === '') return;//去除所有均为空的情况

        $(this).parent().parent().children().eq(0).children("input[type='text']").val(relationname[0]);
        $(this).parent().parent().children().eq(1).children("input[type='text']").val(relationname[1]);

        $(this).parent().parent().parent().children().show();
        $(this).parent().parent().hide();
        mutex_input_dblclick = 0;
    });

    revise_show_change_submit = function(that,dir,checkLegal){
        var value = {state:false};
        value.new = $(that).parent().parent().children("input[type='text']").val();
        value.type = $(that).parent().parent().parent().children("label").text();
        if(value.new.replace( /^[" "|"　"]*/, "") === '') return value;//去除所有均为空的情况

        var targetId = treeItem.searchByName(value.new ,m_element.type.toLowerCase());
        if(targetId != undefined)    return alert("Illegal Input");

        value.state = true;

        var text = $(that).parent().parent().parent().parent().children(".row").children("div").children("p").eq(0);
        value.old = $(text).text();
        $(text).text(value.new);
        $(that).parent().parent().parent().hide();
        $(that).parent().parent().parent().parent().children(".row").show();
        mutex_input_dblclick = 0;
        return value;
    };

    $(".icd-element-input-submit").live("click",function(){
        statusArray['attribute']={
            value : $(this).parent().parent().parent().parent().parent().children().eq(0).find("p").text(),
            id :$(this).parent().parent().parent().parent().parent().parent().parent().children("input[type='hidden']").val()
        }
        var dir = null;
        //如果不是attributeName之后会修正
        var value = revise_show_change_submit($(this),icd.class[statusArray.class.id].attribute,true);
        var that = $(this).parent().parent().parent().parent();
        //无需做重复性检验这里只是为了统一
        if(value.state === true){
            if(chToEn[value.type] === "multiplicity"){
                value.new = multiTransform.changeToShow(value.new);
                value.old = multiTransform.changeToShow(value.old);
                //因为直接出发的是不对的
                var showElem = multiTransform.changeToPost(value.new);
                $(this).parent().parent().parent().parent().children(".recommend-element").children(".span4").children("p").text(showElem);
                $(this).parent().parent().children("input[type='text']").val(showElem)
            }
            statusArray['attributeElem'] = {
                type : chToEn[value.type],
                id : value.new
            }
            var type="attributeElem";
            if(chToEn[value.type] === "name"){
                type = "attributeName";//此处因为是上传数据。。。
            }
            $.ajax({
                url: '/post/data/icd-element',
                type: 'post',
                data: {
                    statusArray:statusArray,
                    type : type,
                    process : 'revise',
                    data: {
                        old:value.old,
                        sub :icd['class'][statusArray.class.id]['attribute'][statusArray.attribute.id][statusArray.attributeElem.type][value.old],
                        new:value.new
                    }
                },
                dataType: 'json',
                async: true,
                success: function(doc){
                    icd = doc.icd;
                    getSourceNav();
                    if(doc.count != 1) return alert("Fail! Please refresh and try it again");
                    summaryIntoString($(that).parent().parent().parent());
                    $(that).children(".recommend-element").trigger("click");
                }
            });
        }
    });

    $(".icd-element-input-relation-type-submit").live("click",function(){
        var value={};
        value.new = $(this).parent().parent().children("label").children("input[type='radio']:checked").val();
        var text = $(this).parent().parent().parent().children(".row").children(".span4").children("p");
        value.old = $(text).text();
        $(text).text(value.new);

        value.new = chToEn[value.new];
        value.old = chToEn[value.old];

        $(this).parent().parent().hide();
        $(this).parent().parent().parent().children(".row").show();
        mutex_input_dblclick=0;
        var url='/post/html/icd-element-relation-sub-'+value.new;

        $.ajax({
            url: '/post/data/icd-element',
            type: 'post',
            data: {
                statusArray:statusArray,
                type : 'relationType',
                process : 'revise',
                data: {
                    old:value.old,
                    sub :icd['relation'][statusArray['relation']][value.old],
                    new:value.new
                }
            },
            dataType: 'json',
            async: true,
            success: function(doc){
                icd = doc.icd;
                getSourceNav();
                if(doc.count != 1) return alert("Fail! Please refresh and try it again");
                $.ajax({
                    url: url,
                    type: 'post',
                    data: {},
                    success: function(html){
                        $("#icd-relation-detail").children().remove();
                        $("#icd-relation-detail").append(html);
                        var className = statusArray['relation'].split("-");
                        icd_element_relationType_show[value.new]($("#icd-relation-detail"),className,icd['relation'][statusArray['relation']][value.new]);
                    }
                });
            }
        });
    });

    $(".icd-element-input-relation-type-cancel").live("click",function(){
        var checked_val = $(this).parent().parent().parent().find("p").text();
        $(this).parent().parent().children("label").children("input[type='radio'][value="+checked_val+"]").attr('checked','checked');

        $(this).parent().parent().hide();
        $(this).parent().parent().parent().children(".row").show();
        mutex_input_dblclick=0;
    });

    $(".icd-element-input-multi1-submit").live("click",function(){
        multi_submit($(this),'multiply1');
    });

    $(".icd-element-input-multi2-submit").live("click",function(){
        multi_submit($(this),'multiply2');
    });

    multi_submit = function(that,multiType){
        var value = revise_show_change_submit($(that),null,false);
        statusArray['relationElem'] = multiType;
        if(value.state === true){
            $.ajax({
                url: '/post/data/icd-element',
                type: 'post',
                data: {
                    statusArray:statusArray,
                    type : 'relationElemValue',
                    process : 'revise',
                    data: {
                        old:value.old,
                        sub :icd['relation'][statusArray['relation']][statusArray['relationType']][multiType][value.old],
                        new:value.new
                    }
                },
                dataType: 'json',
                success: function(doc){
                    icd = doc.icd;
                    getSourceNav();
                    if(doc.count != 1) return alert("Fail! Please refresh and try it again");
                }
            });
        }
    };
});

//accordion-group top lable
$(function(){
    var this_group = '';
    var alert_type = '';
    var alert_name = '';
    var alert_id = '';

    $(".accordion-heading .row").live('click',function(){
        $(this).parent().parent().parent().children(".accordion-body").toggle();
    })
    //这是relation层的，为什么导致上面直接用accordion-heading的不工作，不太清楚
    $(".accordion-heading.row").live('click',function(){
        $(this).parent().children(".accordion-body").toggle();
    })

    $(".accordion-heading .span4").live("click",function(){
        //$(this).parent().parent().parent().parent().children(".accordion-body").toggle();
    });

    $(".recommend-attribute .icon-zoom-in").live("click",function(){
        //$(this).parent().parent().parent().parent().parent().parent().children(".accordion-body").toggle();
    });

    $("#rintra-content .icon-trash").live("click",function(){
        $("#testTree").find(".active .icon-trash").trigger('click');
    });

    $("#rinter-content .icon-trash").live("click",function(){
        this_group = $(this).parent().parent().parent().parent().parent().parent();
        alert_type = "Rinter";
        alert_name =  $(this_group).children(".accordion-heading").children().children().children().eq(0).children("p").text();

        $('#modal-alert-element').modal('show');
    });

    $('#modal-alert-element').on('show', function () {
        $("#modal-alert-element").children(".modal-body").children("h5").text("Delete  "+alert_type+" : "+alert_name+"?");
    });

    $('#delete-element').live('click',function(){
        var status_type;
        var attribute_id,alert_id;

        if(alert_type === 'Rinter'){
            var textItem = $(this_group).children(".accordion-heading").children().children().children().eq(0).children("p");
            var relationType = $(textItem).children("strong").text();
            var targetId = $(this_group).children("input[type='hidden']").val();
            var data = {
                process : 'remove',
                relation : relationType,
                source: {
                    id : m_element.id,
                    type : m_element.type
                },
                target : {
                    id : targetId,
                    type :relationExchangeIntoOperation(relationType)
                }
            };
            ajax_relationRemove(data,$(this_group));
        }else{

        }
    });

    $('#delete-relation-type').live('click',function(){
        //id 用户删除，type用户定性，id用户定位
        //sub层面精确到id的下一层
        var element = statusArray.relationType;
        var sub = icd['relation'][statusArray.relation.id][statusArray.relationType.type][statusArray.relationType.id];
        $.ajax({
            url: '/post/data/icd-element',
            type: 'post',
            data: {
                statusArray:statusArray,
                type : 'relationType',
                process : 'remove',
                data: {
                    new:element,
                    sub:sub
                }
            },
            dataType: 'json',
            async: true,
            success: function(doc){
                icd =doc.icd;
                if(doc.count != 1) return alert("Fail! Please refresh and try it again");
                else $(this_group).remove();
            }
        });
    });


    $(".recommend-attribute .icon-plus-sign").live("click",function(){
        $(this).parent().parent().parent().parent().parent().parent().children(".accordion-body").toggle();
        this_group = $(this).parent().parent().parent().parent().parent().parent();
        var attribute = $(this_group).children("input[type='hidden']").val();
        //Name Reform
        //for(var key in icd['class'][statusArray.class.id]['attribute'][attribute]['attributeName']){
        for(var key in icd['class'][statusArray.class.id]['attribute'][attribute]['name']){
            statusArray['attribute'] = {
                id : attribute,
                value : key
            }
            break;
        }

        this_group = $(this).parent().parent().parent().parent().parent().parent();
        alert_type = "attribute";
        alert_name =  $(this_group).children(".accordion-body").children(".accordion-inner ").children(".control-group").eq(0).find(".span4").children("p").text();

        $('#modal-attribute-add').modal('show');
    });

    $('#modal-attribute-add').on('show', function () {
        //$("#modal-attribute-add").children(".modal-header").children("h5").text('Create element of Attribute"'+alert_name+'"');
        //$("#modal-attribute-add").children(".modal-body").children(".btn-group").children("button").html('Select  ...  <span class="caret"></span>');

        //初始化
        alert_name = $("#ifm-element-name").children(".control-group").eq(0).find("p").eq(0).text();//类的名字
        $("#modal-attribute-add").children(".modal-header").children("h5").text('Create relation of Entity "'+alert_name+'"');
        $("#modal-attribute-add").children(".modal-body").children(".btn-group").children("button").html('Select  ...  <span class="caret"></span>');
        var ul = $("#modal-attribute-add").children(".modal-body").children(".btn-group").children("ul");
        $(ul).children().remove();

        var type = $("#modal-attribute-add").children(".modal-body").children(".btn-group").children("button").text;

        for(var i=0;i<rinter[m_element.type.toLowerCase()].length;i++){
            $(ul).append("<li value="+rinter[m_element.type.toLowerCase()][i]+"><a href='javascript:;'>"+enToCh[rinter[m_element.type.toLowerCase()][i]]+"</a></li>");
        }

        $("#modal-attribute-add").children(".modal-body").children("input[type=text]").val("");

        //
        if(m_element.relation != null){
            //选中type
            if(m_element.relation.type === "is-a"){
                $("#modal-attribute-add").children(".modal-body").children(".btn-group").children("button").html('is-a<span class="caret"></span>');
                $("#modal-attribute-add").children(".modal-body").children(".btn-group").children("ul").children().remove();
            }
            else{
                var lis = $("#modal-attribute-add").children(".modal-body").children(".btn-group").children("ul").children("li");
                for(var i=0;i<lis.length;i++){
                    if(lis.eq(i).text() === m_element.relation.type) lis.eq(i).trigger('click');
                }
            }
            //填充input
            $("#modal-attribute-add").children(".modal-body").children("input[type=text]").val(m_element.relation.element[1]);
        }
    });

    //trust
    $("#attribute-add").live("click", function(){
        //输入
        var relationType = $(this).parent().parent().parent().children(".modal-body").children(".btn-group").children("button").text();
        var targetName = $(this).parent().parent().parent().children(".modal-body").children("input[type='text']").val();
        //数据转换
        var targetType = relationExchangeIntoOperation(relationType);
        var targetId = treeItem.searchByName(targetName,targetType);
        if(targetId === undefined)    return alert("Illegal Input");
        //ajax
        var data = {
            process : 'add',
            relation : relationType,
            source: {
                id : m_element.id,
                type : m_element.type
            },
            target : {
                id : targetId,
                name : targetName,
                type : targetType
            }
        };
        return ajax_relationAdd(data);
    });

    $("#attribute-revise").live("click", function(){
        //输入
        var relationType = $(this).parent().parent().parent().children(".modal-body").children(".btn-group").children("button").text();
        var targetName = $(this).parent().parent().parent().children(".modal-body").children("input[type='text']").val();
        //数据转换
        var targetType = relationExchangeIntoOperation(relationType);
        var targetId = treeItem.searchByName(targetName,targetType);
        if(targetId === undefined)    return alert("Illegal Input");
        //ajax
        var data = {
            process : 'revise',
            relation : relationType,
            source: {
                id : m_element.id,
                type : m_element.type
            },
            target : {
                new : {
                    id : targetId,
                    name : targetName,
                    type : targetType
                },
                old :{
                    id : m_element.relation.id,
                    name : m_element.relation.name,
                    type : targetType
                }
            }
        };
        return ajax_relationRevise(data,m_element.relation.item);
    });

    $('#modal-single-select').on('show', function () {
        if(m_recommendAdd){
            //通过推荐引用则不支持新建
            m_recommendAdd = false;
            $(this).find("#new-single-element").hide();
        }else{
            $(this).find("#new-single-element").show();
        }
        //数据处理
        $("#modal-single-select").children(".modal-header").html('<h5>Create '+chooseList.type+'：'+chooseList.name+'</h5>');
        var item = $("#modal-single-select").children(".modal-body");
        item.children().remove();
        switch(chooseList.typeEn){
            case 'class':
                for(var i=0;i<chooseList.array.length;i++){
                    //classModelOption(chooseList.dir,chooseList.array[i],item);
                    ModelOptionFrame(chooseList.dir,chooseList.array[i],item,'class');
                    if(i>=3) {
                        $(item).append('<div style="height:25px;text-align: center;">more...</div>');
                        break;
                    }
                }
                break;
            case 'attribute':
                for(var i=0;i<chooseList.array.length;i++){
                    ModelOptionFrame(chooseList.dir,chooseList.array[i],item,'attribute');
                    if(i>=3) {
                        $(item).append('<div style="height:25px;text-align: center;">more...</div>');
                        break;
                    }
                }
                break;
        }
    });

    $('#modal-single-select .accordion-heading').live('click',function(){
        $(this).parent().children(".accordion-body").toggle();
    })
    $('#modal-single-select input[type="radio"]').live('click',function(){
        $(this).parent().parent().parent().children(".accordion-body").toggle();
        //radio
        $('#modal-single-select input[type="radio"]').attr('checked',false);
        $(this).attr('checked',true);
    });

    //这个是relationType中的选项
    $('.optional .accordion-heading').live('click',function(){
        $(this).parent().children(".accordion-body").toggle();
    });
    $('.optional input[type="radio"]').live('click',function(){
        //radio
        $('.optional input[type="radio"]').attr('checked',false);
        $(this).attr('checked',true);

        var itemGroup = $(this).parent().parent().parent();
        $(itemGroup).children(".accordion-body").show();//先show所以可以获取
        //从title信息获取数据
        var title = $(itemGroup).children(".accordion-heading").children(".span9").children("p").eq(1)//.text();
        $(title).children().text(" ");
        var selectedTypeTitle = splitRelationTypeTitle(title.text());
        selectedTypeTitle.id = $(itemGroup).children("input[type=hidden]").val();
        selectShow($(itemGroup).parent().parent(),selectedTypeTitle);
        //从表格信息获取数据
        var group = $(itemGroup).find(".accordion-group");
        var typeValue = getCreateRelationTypeValue(group);  //获取所有元素信息
        if(typeValue === undefined) return; //说明数据存在问题
        selectDetailShow($(itemGroup).parent().parent(),typeValue);
        $(itemGroup).children(".accordion-body").hide();//后hide
    });



    $("#new-single-element").live('click',function(){
        var data = {
            statusArray:statusArray,
            type : chooseList.typeEn,
            process : 'add',
            data: {
                new: {
                    value : chooseList.name,
                    id : 'id'
                }
            }
        };
        switch(chooseList.typeEn){
            case 'class':ajax_classAdd(data);
                break;
            case 'attribute':ajax_attributeAdd(data,this_group);
                break;
            default:break;
        }
    });

    $("#select-single-element").live('click',function(){
        var itemGroup = $('#modal-single-select input[type="radio"]:checked').parent().parent().parent();
        if(itemGroup.length != 1) return alert('Choose one before click button "select"');
        var data = {
            statusArray:statusArray,
            type : chooseList.typeEn,
            process : 'cite',
            data: {
                new: {
                    value : chooseList.name,
                    id : $(itemGroup).children("input[type=hidden]").val()
                }
            }
        };
        switch(chooseList.typeEn){
            case 'class':ajax_classAdd(data);
                break;
            case 'attribute':ajax_attributeAdd(data,this_group);
                break;
            default:break;
        }
    })

    $(".relationType-plus").live('click',function(){
        statusArray.relationType.id = undefined;
        $('#modal-create-relationType').modal('show');
    })

    $('#modal-create-relationType').on('show', function(){
        $.ajax({
            url: '/post/html/icd-element-relation-sub',
            type: 'post',
            async: false,
            success: function(html){
                if(m_relationTypeAddHoldOn) return m_relationTypeAddHoldOn=false ;//表示继续进行前一次修改

                $("#modal-create-relationType").children(".modal-body").children().remove();
                $("#modal-create-relationType").children(".modal-body").append(html);
                var accordionGroup = $('#modal-create-relationType').children(".modal-body").children()
                relationSubToRevise(accordionGroup); //基本内容初始化逻辑
            }
        });
    })

    $('.table-relation li').live('click',function(){
        relationSubTypeReform($(this));
        //获得一个中间量 寄存使用
        TypeAheadState.relationTYpe.type = $(this).attr('name');
        if(TypeAheadState.relationTYpe.type === 'association'){
            relationSubOptional(TypeAheadState.relationTYpe.type,
                $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().children(".optional"));
        }
    })

    $("#create-relationType").live('click',function(){
        //获得页面所有元素值
        if(m_relationTypeRevise === false){
            var group = $('#modal-create-relationType').find(".accordion-group")
            var typeValue = getCreateRelationTypeValue(group);  //获取所有元素信息
            if(typeValue === undefined) return; //说明数据存在问题
            //生成上传数据信息
            //同时更新了statusArray
            var alertStr = getCreateRelationPostValue(typeValue);
            if(alertStr != undefined) return alert(alertStr);

            var checkItem = icd.relation[statusArray.relation.id];
            if(checkItem[statusArray.relationType.type] != undefined){
                if(statusArray.relationType.type === 'association'){
                    //break;
                }
                if(checkItem[statusArray.relationType.type][statusArray.relationType.id] != undefined){
                    for(var order in statusArray.relationType.sub['order']){
                        if(checkItem[statusArray.relationType.type][statusArray.relationType.id].order[order] != undefined){
                            return alert("Fail！Already exist")
                        }
                    }
                }
            }
            $("#modal-create-relationType").modal("hide");
            var data = {
                statusArray:statusArray,
                type : 'relationType',
                process : 'multiCite',
                data: {
                    new: statusArray.relationType
                }
            };
            var itemGroup = $("#icd-relation-name").parent().children(".accordion");
            ajax_relationTypeAdd(data,itemGroup);
        }else{
            var old =  statusArray.relationType;
            var group = $('#modal-create-relationType').find(".accordion-group")
            var typeValue = getCreateRelationTypeValue(group);  //获取所有元素信息
            var alertStr = getCreateRelationPostValue(typeValue);
            if(alertStr != undefined) return alert(alertStr);
            $("#modal-create-relationType").modal("hide");
            var data = {
                statusArray:statusArray,
                type : 'relationType',
                process : 'multiRevise',
                data: {
                    new: statusArray.relationType,
                    old: old
                }
            };
            ajax_relationTypeAdd(data,m_relationTypeGroup);
            $(m_relationTypeGroup).remove();
        }
    });
    ///
    $(".btn-group.controls-small li").live("click",function(){
        var this_value = $(this).children("a").text();
        $(this).parent().parent().children("button").html(this_value+'<span class="caret"></span>');
        switch(chToEn[this_value]){
            case 'ordering': case 'uniqueness':case 'readOnly':case 'union':case'composite':
                $(this).parent().parent().parent().children("input[type='text']").hide();
                $(this).parent().parent().parent().children(".control-sub").show();
                break;
            default :
                $(this).parent().parent().parent().children("input[type='text']").show();
                $(this).parent().parent().parent().children(".control-sub").hide();
                break;
        }
    });

    $(".control-sub input[type='radio']").live('click',function(){
        $(this).parent().parent().find("input[type='radio']").attr('checked',false);
        $(this).attr('checked',true);
    });

    $(".content-detail .control-sub input[type='radio']").live('click',function(){
        var val = $(this).val();
        $(this).parent().parent().parent().children("input[type='text']").val(val);
    });

    $(".attribute-plus").live("click",function(){
        m_element.relation = null;
        $('#modal-attribute-add').modal('show');
    });

    $(".recommend-attribute .icon-plus").live("click",function(){
        $(this).parent().parent().parent().parent().parent().parent().children(".accordion-body").toggle();
        this_group = $(this).parent().parent().parent().parent().parent().parent();
        $('#modal-create-attribute').modal('show');
    });

    $("#create-attribute").live("click", function(){
        var item_val = $(this).parent().parent().parent().children(".modal-body").children("input[type='text']").val();
        var list = $(".form-horizontal").find(".accordion").find(".accordion-inner").find("p:first");
        if(checkInIcd(list,item_val) === true) return;
        //var dir = 'class.'+statusArray.class.id+'.attribute'
        var array = checkInCcd(icd['class'][statusArray.class.id]['attribute'],ccd['class'][statusArray.class.id]['attribute'],item_val,'attribute');
        var process,elem={};
        if(array.length === 0) {
            var data = {
                statusArray:statusArray,
                type : 'attribute',
                process : 'add',
                data: {
                    new:{
                        value : item_val,
                        id : 'id'
                    }
                }
            }
            ajax_attributeAdd(data,this_group);
        }else{
            setChooseList(item_val,'attribute',array);
            $('#modal-single-select').modal('show');
        }

    });

    //according-heading
    $(".recommend-attribute .icon-arrow-up").live("click",function(){
        var this_root = $(this).parent().parent().parent().parent().parent().parent();
        var html = '<div class="accordion-group">'+$(this_root).html()+'</div>';
        if($(this_root).prev().hasClass("accordion-group")){
            $(this_root).prev().before(html);
            $(this_root).remove();
        }
    });

    $(".recommend-attribute .icon-arrow-down").live("click",function(){
        //$(this).parent().parent().parent().parent().parent().parent().children(".accordion-body").toggle();

        var this_root = $(this).parent().parent().parent().parent().parent().parent();
        var html = '<div class="accordion-group">'+$(this_root).html()+'</div>';
        if($(this_root).next().hasClass("accordion-group")){
            $(this_root).next().after(html);
            $(this_root).remove();
        }
    });

    $(".recommend-element .icon-trash").live("click",function(){
        //$(this).parent().parent().parent().parent().parent().parent().children(".accordion-body").toggle();
        this_group = $(this).parent().parent().parent().parent();
        var span4 =  $(this_group).children(".row").children(".span4");
        alert_type = $(span4).children("label").text();
        alert_name = $(span4).children("p").text();
        alert_id = statusArray.attribute.id;
        $('#modal-alert-element').modal('show');
    });

    $(".icon-refresh.source").live("click",function(){
        var item={};
        item.label = $(this).parent().parent().children("label").text();
        item.value = $(this).parent().parent().children("input").val();

        var ccd_dir;
        var array;
        $("#modal-source-detail").children(".modal-body").children(".detail-info").children().remove();
        switch(item.label){
            case "类":
                ccd_dir = ccd["class"][item.value];
                for(var key in ccd_dir){
                    if(regex.test(key) === false){
                        $("#modal-source-detail").children(".modal-body").children(".detail-info").append(
                            "<label class='control-label-small controls-small'>Attribute:</label>"
                        +"<p class='controls-text-small' style='padding-left: 75px'>"+key+"</p>")
                    }
                };
                break;
            case "属性":
                ccd_dir = ccd['class'][statusArray['class']][item.value];
                for(var key in ccd_dir){
                    if(regex.test(key) === false){
                        for(var val in ccd_dir[key]){
                            if(regex.test(val) === false){
                                $("#modal-source-detail").children(".modal-body").children(".detail-info").append(
                                    "<label class='control-label-small controls-small'>"+enToCh[key]+":</label>"
                                +"<p class='controls-text-small' style='padding-left: 75px'>"+val+"</p>")
                            }
                        }
                    }
                };
                break;
            default: break;
        }
    });

    $(".icon-ok.source").live("click",function(){
        var item={};
        item.label = $(this).parent().parent().children("label").text();
        item.value = $(this).parent().parent().children("input").val();
        item.exist = false;

        generateItem(item);
        if(item.exist)  return alert("Fail! Already Exist")
        $.ajax({
            url: '/post/data/icd-element',
            type: 'post',
            data: {
                statusArray:statusArray,
                type : item.type,
                process : 'revise',
                data: {
                    old:modalSourceValue,
                    sub :item.sub,
                    new:item.value
                }
            },
            dataType: 'json',
            success: function(doc){
                icd = doc.icd;
                getSourceNav();
                if(doc.count != 1) return alert("Fail! Please refresh and try it again");
                updateSource(item.label,item.value);
            }
        });
    })

    /*
    $("#rintra-content .icon-edit").live('click',function(){
        this_group = $(this).parent().parent().parent().parent().parent().parent();
        alert_type = "rintra";
        alert_name =  $(this_group).children(".accordion-heading").children().children().children().eq(0).children("p").text();

    });

    $("#rinter-content .icon-edit").live('click',function(){
        this_group = $(this).parent().parent().parent().parent().parent().parent();
        alert_type = "rinter";
        alert_name =  $(this_group).children(".accordion-heading").children().children().children().eq(0).children("p").text();
    });
    */


    $("#modal-create-relationType .table-relation input[type='radio']").live('click',function(){
        $(this).parent().parent().find("input[type='radio']").attr('checked',false);
        $(this).attr('checked',true);
    });

    $("#modal-create-relationType .icon-plus-sign").live('click',function(){
        m_relationTypeAddHoldOn = true;
        //进行时要check当前的状态
        $("#modal-create-relationType").modal('toggle');
        $("#modal-relationType-add").modal('show');
    });

    $("#modal-relationType-add").on('show', function(){
        var controls = $("#modal-relationType-add").children(".modal-body").children(".control-group").children(".controls");
        $(controls).children().remove();
        //寻找一下
        //var hiddenTr = $("#modal-create-relationType").find(".additional").children("tr:hidden");
        var hiddenTr = $("#modal-create-relationType").find(".additional").eq(0).children("tr:hidden");
        for(var i=0;i<hiddenTr.length;i++){
           var title = $(hiddenTr).eq(i).children("td").eq(0).text();
            if(i%3 === 0) $(controls).append("<div></div>");
            $(controls).append(modalRelationTypeAdd.checkbox(chToEn[title],title));
        }
        $(controls).append(modalRelationTypeAdd.note());
    });

    $("#relationType-add").live('click',function(){
        //添加逻辑改为hide和show
        var checkedItem = $("#modal-relationType-add").find("input[type=checkbox]:checked");
        var checkedList = {};
        for(var i=0;i<checkedItem.length;i++){
            var val = $(checkedItem).eq(i).val();
            checkedList[val] = 1;
        }

        var hiddenTr = $("#modal-create-relationType").find(".additional").eq(0).children("tr:hidden");
        for(var i=0;i<hiddenTr.length;i++){
            var title = $(hiddenTr).eq(i).children("td").eq(0).text();
            if(checkedList[chToEn[title]] != undefined){
                $(hiddenTr).eq(i).show();
                $(hiddenTr).eq(i).find(".show").hide();
                $(hiddenTr).eq(i).find(".control-sub").show();
            }
        }

        $("#modal-create-relationType").modal('show');
    });

    $("tr .icon-minus").live("click",function(){
        $(this).parent().parent().parent().parent().hide();
    })

    $("#create-relationType-add-cancel").live('click',function(){
        $("#modal-create-relationType").modal('show');
    })

    $('#modal-alert-relation-type').on('show', function () {
        $("#modal-alert-relation-type").children(".modal-body").children("h5").text("Delete  "+alert_type+" : "+alert_name+"?");
    });

    $("#modal-create-relationType .icon-resize-horizontal").live("click",function(){
        var tr = $(this).parent().parent().parent().parent().parent();
        relationNameExchange(tr);
    })

    generateItem = function(item){
        switch(item.label){
            case "类":
                item.type = 'class';
                item.sub = icd['class'][statusArray['class']];
                if(icd['class'][item.value] != undefined) item.exist=true;
                break;
            case "属性":
                item.type = 'attribute';
                item.sub = icd['class'][statusArray['class']][statusArray['attribute']];
                if(icd['class'][statusArray['class']][item.value] != undefined) item.exist=true;
                break;
            default: break;
        }
    }
    updateSource = function(label,value){
        switch(label){
            case "类":
                $(".bs-docs-sidenav .active").attr('name',value);
                $(".bs-docs-sidenav .active").trigger('click');
                break;
            case "属性":
                $(recommenditem).children("input[type='hidden']").val(value);
                $(recommenditem).find(".recommend-attribute").trigger('click');
                break;
            default: break;
        }
    }
});




